package com.lbs.jiepiaisi.biz;
import android.content.Intent;
import android.util.Log;

import com.lbs.jiepiaisi.bean.LoginFailBean;
import com.lbs.jiepiaisi.bean.UserEntity;
import com.lbs.jiepiaisi.bean.VinList;
import com.lbs.jiepiaisi.manager.App;
import com.lbs.jiepiaisi.parser.LoginAccountParser;
import com.lbs.jiepiaisi.utils.Constant;
import com.lbs.jiepiaisi.utils.HttpUtil;
import com.lbs.jiepiaisi.utils.UrlUtil;
import org.json.JSONObject;
import okhttp3.FormBody;
import okhttp3.Response;
/**
 *
 * 时间：2017/8/7 15:24
 * 作用：账号登录业务类
 */

public class LoginAccountBiz {
    public void loginBiz(UserEntity userEntity) {
        String account_number=userEntity.getAccount_number();
        String account_password=userEntity.getAccount_password();
        FormBody formBody = new FormBody
                .Builder()
                .add("loginName",account_number)//设置参数名称和参数值
                .add("password",account_password)//设置参数名称和参数值
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_LOGIN_URL,formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                try{
                    String jsonString = response.body().string();//返回结果
                    Log.e("Login:",jsonString);
                    JSONObject jsonObject = new JSONObject(jsonString);//转换结果
                    if(jsonString.toString().contains("\"status\":1")){
                        UserEntity userEntity= new UserEntity();//json解析
                        userEntity.setCode(300);
                        Intent intent = new Intent(Constant.ACCOUNT_LOGIN);
                        intent.putExtra(Constant.KEY_DATA, userEntity);
                        App.instance.sendBroadcast(intent);
                    }else {
                        UserEntity userEntity= LoginAccountParser.parser(jsonObject.toString());//json解析
                        Intent intent = new Intent(Constant.ACCOUNT_LOGIN);
                        intent.putExtra(Constant.KEY_DATA, userEntity);
                        App.instance.sendBroadcast(intent);
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void failed() {
                UserEntity userEntity=new UserEntity();
                userEntity.setCode(Constant.STATUS_NETWORK_ERROR);
                Intent intent = new Intent(Constant.ACCOUNT_LOGIN);
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                UserEntity userEntity=new UserEntity();
                userEntity.setCode(Constant.STATUS_NO_NET);
                Intent intent = new Intent(Constant.ACCOUNT_LOGIN);
                intent.putExtra(Constant.KEY_DATA, userEntity);
                App.instance.sendBroadcast(intent);
            }
        });
    }
    public void loginByCarBiz(UserEntity userEntity) {
        String account_number=userEntity.getAccount_number();
        String account_password=userEntity.getAccount_password();
        FormBody formBody = new FormBody
                .Builder()
                .add("vin",account_number)//设置参数名称和参数值
                .add("password",account_password)//设置参数名称和参数值
                .build();
        HttpUtil.getHttpUtil().post(UrlUtil.ACCOUNT_VIN_URL,formBody, new HttpUtil.Listener() {
            @Override
            public void success(Response response) {
                try{
                    String jsonString = response.body().string();//返回结果
                    Log.e("VIN:",jsonString);
                    if(jsonString.contains("\"status\":1")){
                        LoginFailBean o = LoginAccountParser.parserLoginFail(jsonString);//json解析
                        VinList v = new VinList();
                        v.setResult(null);
                        System.out.println("ceshi1");
                        v.setMessage("帐号或密码错误，请重新登录！");
                        v.setStatus(Constant.STATUS_NULL + "");
                        Intent intent = new Intent(Constant.ACCOUNT_VIN_LOGIN);
                        intent.putExtra(Constant.KEY_DATA, v);
                        App.instance.sendBroadcast(intent);
                    }else {
                        System.out.println("ceshi2");
                        VinList vinList = LoginAccountParser.parserVin(jsonString);//json解析
                        vinList.setStatus(Constant.STATUS_OK + "");
                        Intent intent = new Intent(Constant.ACCOUNT_VIN_LOGIN);
                        intent.putExtra(Constant.KEY_DATA, vinList);
                        App.instance.sendBroadcast(intent);
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void failed() {
                VinList vinList =new VinList();
                vinList.setStatus(Constant.STATUS_NETWORK_ERROR + "");
                Intent intent = new Intent(Constant.ACCOUNT_VIN_LOGIN);
                intent.putExtra(Constant.KEY_DATA, vinList);
                App.instance.sendBroadcast(intent);
            }
            @Override
            public void noNetwork() {
                VinList vinList =new VinList();
                vinList.setStatus(Constant.STATUS_NO_NET+"");
                Intent intent = new Intent(Constant.ACCOUNT_VIN_LOGIN);
                intent.putExtra(Constant.KEY_DATA, vinList);
                App.instance.sendBroadcast(intent);
            }
        });
    }
}
